package com.team8.system.service.listener;

import com.team8.system.pojo.FlightInfo;
import com.team8.system.pojo.TicketRecord;
import com.team8.system.service.dao.FlightInfoDao;
import com.team8.system.service.dao.TicketRecordDao;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Component
    public class RefreshDBListener {


        @Autowired
        private StringRedisTemplate redisTemplate;

        @Autowired
        private FlightInfoDao flightInfoDao;

        @Autowired
        private TicketRecordDao ticketRecordDao;


    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(
                    value = "as.refreshdb.flightinfos",
                    durable = "true"
            ),     // 声明队列
            exchange = @Exchange(
                    value = "as.refreshdb.exchange",
                    ignoreDeclarationExceptions = "true",
                    type = ExchangeTypes.TOPIC
            ),
            key = {"db.flightinfo.update"}
    )
    )
    @Transactional
    public void listenFlightInfoRefresh(String msg) throws Exception {
        String[] split = msg.split(":");
        String mqFfid = split[0];
        String mqStartSeg = split[1];
        String mqEndSeg = split[2];
        String mqTicketNum = split[3];

        String key = mqFfid + ":" + mqStartSeg + ":" + mqEndSeg;
        // 分段的格式是 ffid startSeg endSeg ticketNum
        String ticket = redisTemplate.opsForValue().get(key);

        // 如果redis中的票和接收到的消息的票一直，睡1秒钟
        if(ticket.equals(mqTicketNum)){
            Thread.sleep(1000);

            // 睡一秒钟之后如果还一致，更新数据库
            ticket = (String)redisTemplate.opsForValue().get(key);

            if(ticket.equals(mqTicketNum)){
                flightInfoDao.updateTicketNum(mqFfid, mqStartSeg, mqEndSeg, Integer.valueOf(ticket));
            }
        }
    }


    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(
                    value = "as.refreshdb.ticketrecord",
                    durable = "true"
            ),     // 声明队列
            exchange = @Exchange(
                    value = "as.refreshdb.exchange",
                    ignoreDeclarationExceptions = "true",
                    type = ExchangeTypes.TOPIC
            ),
            key = {"db.ticketrecord.booking"}
    )
    )
    @Transactional
    public void listenBooking(String msg) throws Exception {

        // ffid startseg endseg emails version
        String[] split = msg.split(":");
        String mqFfid = split[0];
        String mqStartSeg = split[1];
        String mqEndSeg = split[2];
        String mqEmail = split[3];
        System.out.println(msg);
        TicketRecord ticketRecord = new TicketRecord();

        Example example = new Example(FlightInfo.class);

        Example.Criteria criteria = example.createCriteria();

        criteria.andEqualTo("ffid", mqFfid);
        criteria.andEqualTo("startSeg", mqStartSeg);
        criteria.andEqualTo("endSeg", mqEndSeg);

        List<FlightInfo> flightInfos = flightInfoDao.selectByExample(example);

        if(!CollectionUtils.isEmpty(flightInfos)){
            FlightInfo flightInfo = flightInfos.get(0);
            ticketRecord.setStartPos(flightInfo.getStartPort());
            ticketRecord.setEndPos(flightInfo.getEndPort());
        }
        ticketRecord.setFfid(mqFfid);
        ticketRecord.setPersonEmail(mqEmail);
        ticketRecordDao.insert(ticketRecord);
    }
}